import { Component, OnInit, transition } from '@angular/core';
import { FormBuilder, FormGroup, Validators, } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { NzNotificationService } from 'ng-zorro-antd';
import { NzModalService } from 'ng-zorro-antd';

import { UsermanegeService, User } from '../../../DM/usermanege.service';
@Component({
  selector: 'app-user-ls',
  templateUrl: './user-ls.component.html',
  styleUrls: ['./user-ls.component.css'],
  providers: []
})
export class UserLsComponent implements OnInit {

  userLs: User[] = [];
  constructor(
    private fb: FormBuilder,
    private usermanegeService: UsermanegeService,
    private msgService: NzMessageService,
    private noticeService: NzNotificationService,
    private confirmServ: NzModalService,
  ) { }

  async ngOnInit() {
    try {
      this.userLs = await this.usermanegeService.getUserLs();
    } catch (err) { }
  }

  async deleteUser(index: number): Promise<void> {
    try {
      this.confirmServ.confirm({
        title: '您是否确认要删除该用户？',
        content: `<div>id：${this.userLs[index].UserId}</div><div>用户名：${this.userLs[index].UserName}</div><div>用户角色：${this.userLs[index].Role.RoleName}</div>`,
        onOk: async () => {
          await this.usermanegeService.deleteUser(this.userLs[index].UserId);
          this.msgService.success('删除成功');
          this.noticeService.blank('删除用户', `您刚刚删除了用户：${this.userLs[index].UserName}`);
          await this.ngOnInit();
        },
        onCancel() {
        }
      });
    } catch (err) {
      this.msgService.error('删除失败');
      this.noticeService.blank('删除用户', `由于${err}，删除用户失败`);
    }
  }

}
